# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=brotli
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-brotli-testdata")
pkgver=1.0.7
pkgrel=4
pkgdesc="Brotli compression library (mingw-w64)"
arch=('any')
url='https://github.com/google/brotli'
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python")
license=('MIT')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/brotli/archive/v${pkgver}.tar.gz")
sha256sums=('4c61bfb0faca87219ea587326c467b95acb25555b53d1a421ffa3c8a9296ee2c')

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build

  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      ../${_realname}-${pkgver}

  make
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make test
}

package_brotli() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"
  install -D -m644 "${srcdir}"/brotli-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python-brotli() {
  depends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")

  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"

}

package_brotli-testdata() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd brotli-${pkgver}
  install -dm755 "${pkgdir}${MINGW_PREFIX}/share/brotli"
  cp -a tests/testdata "$pkgdir${MINGW_PREFIX}/share/brotli/"
  install -D -m644 "${srcdir}"/brotli-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-testdata/LICENSE"
}

package_mingw-w64-i686-brotli() {
  package_brotli
}

package_mingw-w64-i686-brotli-testdata() {
  package_brotli-testdata
}

package_mingw-w64-i686-python-brotli() {
  package_python-brotli
}

package_mingw-w64-x86_64-brotli() {
  package_brotli
}

package_mingw-w64-x86_64-brotli-testdata() {
  package_brotli-testdata
}

package_mingw-w64-x86_64-python-brotli() {
  package_python-brotli
}
